#include "tcpserver.h"

TcpServer::TcpServer(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("TCP Server"));
    ContentListWidget = new QListWidget;
    PortLabel = new QLabel(tr("端口:"));
    PortLineEdit = new QLineEdit;
    CreateBtn = new QPushButton(tr("创建聊天室"));
    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(ContentListWidget,0,0,1,2);
    mainLayout->addWidget(PortLabel,1,0);
    mainLayout->addWidget(PortLineEdit,1,1);
    mainLayout->addWidget(CreateBtn,2,0,1,2);
    port=8010;
    PortLineEdit->setText(QString::number(port));
    connect(CreateBtn,&QPushButton::clicked,this,&TcpServer::slotCreateServer);
}

TcpServer::~TcpServer()
{
}

void TcpServer::slotCreateServer()
{
    server = new Server (this, port); //创建一个 Server 对象
    connect(server,&Server::updateServer,this,&TcpServer::updateServer); // (a)
    CreateBtn->setEnabled(false);
}

void TcpServer::updateServer(QString msg, int length)
{
    ContentListWidget->addItem(msg.left(length));
}

